<!DOCTYPE html>
<html >
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="潍夫子" />



<meta name="description" content="safeArea定义SafeArea可以看做是系统给所有的view都加上了一个虚拟的view，以这个虚拟view为参考添加的视图对象都能保证不被遮挡而正常显示。
safeArea在不同地方的表现UIView中的safeArea在iOS11中为view新增了关于safeArea的两个属性和一个方法：
@available(iOS 11.0, *)
open var safeAreaInsets: U">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS11 SafeArea适配相关">
<meta property="og:url" content="http://weifuzi.oschina.io/2018/05/30/iOS11Safearea/index.html">
<meta property="og:site_name" content="潍夫子的个人博客">
<meta property="og:description" content="safeArea定义SafeArea可以看做是系统给所有的view都加上了一个虚拟的view，以这个虚拟view为参考添加的视图对象都能保证不被遮挡而正常显示。
safeArea在不同地方的表现UIView中的safeArea在iOS11中为view新增了关于safeArea的两个属性和一个方法：
@available(iOS 11.0, *)
open var safeAreaInsets: U">
<meta property="og:image" content="http://weifuzi.oschina.io/2018/05/30/iOS11Safearea/1.jpg">
<meta property="og:updated_time" content="2018-05-30T09:21:58.554Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS11 SafeArea适配相关">
<meta name="twitter:description" content="safeArea定义SafeArea可以看做是系统给所有的view都加上了一个虚拟的view，以这个虚拟view为参考添加的视图对象都能保证不被遮挡而正常显示。
safeArea在不同地方的表现UIView中的safeArea在iOS11中为view新增了关于safeArea的两个属性和一个方法：
@available(iOS 11.0, *)
open var safeAreaInsets: U">
<meta name="twitter:image" content="http://weifuzi.oschina.io/2018/05/30/iOS11Safearea/1.jpg">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="潍夫子的个人博客" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>iOS11 SafeArea适配相关 | 潍夫子的个人博客</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script> yiliaConfig.jquery_ui = [false]; </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">潍夫子</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:yuyekuangmo@qq.com" title="Email"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                
                

                
                
                <section class="switch-part switch-part3">
                
                    <div id="js-aboutme">没事打酱油</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">潍夫子</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">潍夫子</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:yuyekuangmo@qq.com" title="Email"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="Tags" friends="Friends" about="About Me"/>
</nav>
      <div class="body-wrap"><article id="post-iOS11Safearea" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2018/05/30/iOS11Safearea/" class="article-date">
      <time datetime="2018-05-30T03:24:40.000Z" itemprop="datePublished">2018-05-30</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      iOS11 SafeArea适配相关
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/术业专攻/">术业专攻</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/总结/">总结</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <h2 id="safeArea定义"><a href="#safeArea定义" class="headerlink" title="safeArea定义"></a>safeArea定义</h2><p>SafeArea可以看做是系统给所有的view都加上了一个虚拟的view，以这个虚拟view为参考添加的视图对象都能保证不被遮挡而正常显示。</p>
<h2 id="safeArea在不同地方的表现"><a href="#safeArea在不同地方的表现" class="headerlink" title="safeArea在不同地方的表现"></a>safeArea在不同地方的表现</h2><h3 id="UIView中的safeArea"><a href="#UIView中的safeArea" class="headerlink" title="UIView中的safeArea"></a>UIView中的safeArea</h3><p>在iOS11中为view新增了关于safeArea的两个属性和一个方法：</p>
<pre><code>@available(iOS 11.0, *)
open var safeAreaInsets: UIEdgeInsets { get }
@available(iOS 11.0, *)
open var safeAreaLayoutGuide: UILayoutGuide { get }
@available(iOS 11.0, *)
open func safeAreaInsetsDidChange()
</code></pre><blockquote>
<p>safeAreaInsets和safeAreaLayoutGuide均为只可读属性，在进行布局时可以以这两个属性的任意一个作为参考来进行布局</p>
<p>safeAreaInsetsDidChange是在view的safeArea发生改变后触发的方法</p>
</blockquote>
<p><strong>用途：</strong>最常见的在往viewController的view中添加视图对象时以控制器view的safeArea为参照进行布局，就可以良好适配各种情况的界面展示。此处提供一段UIView的类拓展方法，此段代码依赖第三方框架<em>SnapKit</em>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">import UIKit</div><div class="line">import SnapKit</div><div class="line">public extension UIView &#123;</div><div class="line">    var safeArea: ConstraintBasicAttributesDSL &#123;</div><div class="line">        #if swift(&gt;=3.2)</div><div class="line">        if #available(iOS 11.0, *) &#123;</div><div class="line">            return self.safeAreaLayoutGuide.snp</div><div class="line">        &#125;</div><div class="line">        return self.snp</div><div class="line">        #else</div><div class="line">        return self.snp</div><div class="line">        #endif</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">使用示例：</div><div class="line">make.top.equalTo(self.view.safeArea.top).offset(20)</div></pre></td></tr></table></figure>
<h3 id="UIViewController中的safeArea"><a href="#UIViewController中的safeArea" class="headerlink" title="UIViewController中的safeArea"></a>UIViewController中的safeArea</h3><p>在iOS11中为UIViewController新增了关于safeArea的一个属性和一个方法：</p>
<pre><code>@available(iOS 11.0, *)
open var additionalSafeAreaInsets: UIEdgeInsets
@available(iOS 11.0, *)
open func viewSafeAreaInsetsDidChange()
</code></pre><blockquote>
<p>additionalSafeAreaInsets主要是用在自定义navigationBar、tabBar或是需要为界面留空时使用，修改控制器的该参数会影响控制器根视图view的safeArea</p>
<p>viewSafeAreaInsetsDidChange在控制器根视图的safeArea发生改变后触发</p>
</blockquote>
<p><strong>示例：</strong></p>
<p>self.additionalSafeAreaInsets = UIEdgeInsetsMake(20, 20, 20, 20)</p>
<p><img src="/2018/05/30/iOS11Safearea/1.jpg" alt=""></p>
<h3 id="其他地方的safeArea"><a href="#其他地方的safeArea" class="headerlink" title="其他地方的safeArea"></a>其他地方的safeArea</h3><p>ScrollView、TableView、CollectionView都有一些与safeArea相关的新属性或方法，但只要在将这些view添加到控制器的根视图中时做了safeArea的适配，一般就不需要进行额外的处理，故此处不再做探讨。</p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>Title:</span><a href="/2018/05/30/iOS11Safearea/">iOS11 SafeArea适配相关</a></p>
        <p><span>Author:</span><a href="/" title="Back to Homepage">潍夫子</a></p>
        <p><span>Created:</span>2018-05-30, 11:24:40</p>
        <p><span>Updated:</span>2018-05-30, 17:21:58</p>
        <p>
            <span>Full URL:</span><a class="post-url" href="/2018/05/30/iOS11Safearea/" title="iOS11 SafeArea适配相关">http://weifuzi.oschina.io/2018/05/30/iOS11Safearea/</a>
            <span class="copy-path" data-clipboard-text="From http://weifuzi.oschina.io/2018/05/30/iOS11Safearea/　　By 潍夫子" title="Copy Article&#39;s Link &amp; Author"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>License:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"CC BY-NC-SA 4.0"</a> Keep Link &amp; Author if Distribute.
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2019/03/28/html标签初探/">
                    html标签初探
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2017/03/28/Efficiency-XcodeTemplate/">
                    高效工作--自定义Xcode类模板
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">Contents</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#safeArea定义"><span class="toc-number">1.</span> <span class="toc-text">safeArea定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#safeArea在不同地方的表现"><span class="toc-number">2.</span> <span class="toc-text">safeArea在不同地方的表现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#UIView中的safeArea"><span class="toc-number">2.1.</span> <span class="toc-text">UIView中的safeArea</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#UIViewController中的safeArea"><span class="toc-number">2.2.</span> <span class="toc-text">UIViewController中的safeArea</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#其他地方的safeArea"><span class="toc-number">2.3.</span> <span class="toc-text">其他地方的safeArea</span></a></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="Hide"  title="Show or Hide Table of Contents">

    <script>
        yiliaConfig.toc = ["Hide", "Show", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"iOS11 SafeArea适配相关　| 潍夫子的个人博客　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2019/03/28/html标签初探/" title="Pre: html标签初探">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="Mini Archives"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2017/03/28/Efficiency-XcodeTemplate/" title="Next: 高效工作--自定义Xcode类模板">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2019/04/02/buyHouse/">置业储备</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/03/28/CSS初探/">CSS初探</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/03/28/html标签初探/">html标签初探</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/05/30/iOS11Safearea/">iOS11 SafeArea适配相关</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/03/28/Efficiency-XcodeTemplate/">高效工作--自定义Xcode类模板</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/02/08/iOS-summary/">iOS知识总结<一></a></li><li class="post-list-item"><a class="post-list-link" href="/2017/02/06/interview/">iOS问题总结<二></a></li><li class="post-list-item"><a class="post-list-link" href="/2017/02/05/creatMyBlog/">免费个人博客搭建（Hexo+osChina）</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2017-2019 潍夫子
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="A fast, simple &amp; powerful blog framework">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="Another simple and elegant theme for Hexo  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="Site Visitors"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="Page Hits"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





<div class="scroll" id="scroll">
    <a href="#" title="Back to Top"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="Comments"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="Go to Bottom"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>